﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web.Mvc;


namespace HitchHikersGuideToTheGalaxy.Models
{
    [Bind(Include = "Id,VehicleSortId,Type,SubType,NumberOfPassengers,NumberPlate,Color,FuelId,FuelUsageUrban,FuelUsageRural,FuelUsageCombined")]
    public partial class Vehicle
    {

        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Id.ToString()))
                yield return new RuleViolation("Id is required", "Id");

            if (String.IsNullOrEmpty(VehicleSortId.ToString()))
                yield return new RuleViolation("VehicleSortId is required", "VehicleSortId");

            if (String.IsNullOrEmpty(Type))
                yield return new RuleViolation("Type is required", "Type");

            if (String.IsNullOrEmpty(SubType))
                yield return new RuleViolation("SubType is required", "SubType");

            if (String.IsNullOrEmpty(NumberOfPassengers.ToString()))
                yield return new RuleViolation("NumberOfPassengers is required", "NumberOfPassengers");

            if (String.IsNullOrEmpty(NumberPlate))
                yield return new RuleViolation("NumberPlate is required", "NumberPlate");

            if (String.IsNullOrEmpty(Color))
                yield return new RuleViolation("Color is required", "Color");

            if (String.IsNullOrEmpty(FuelId.ToString()))
                yield return new RuleViolation("FuelId is required", "FuelId");

            if (String.IsNullOrEmpty(FuelUsageUrban.ToString()))
                yield return new RuleViolation("FuelUsageUrban is required", "FuelUsageUrban");

            if (String.IsNullOrEmpty(FuelUsageRural.ToString()))
                yield return new RuleViolation("FuelUsageRural is required", "FuelUsageRural");

            if (String.IsNullOrEmpty(FuelUsageCombined.ToString()))
                yield return new RuleViolation("FuelUsageCombined is required", "FuelUsageCombined");


            yield break;
        }

        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }
}
